package com.qiyou.service;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.google.gson.Gson;
import com.qiyou.dao.IActivityDao;
import com.qiyou.dao.IActivityInvitationDao;
import com.qiyou.dao.factory.DaoFactory;
import com.qiyou.entity.Activity;
import com.qiyou.entity.ActivityInvitation;
import com.qiyou.util.DBUtil;
import com.qiyou.util.LogUtil;

/**
 * 请求参加活动
 */
@WebServlet("/JoinActivityService")
public class JoinActivityService extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private static final Logger log = LogUtil
            .getLogger(JoinActivityService.class);

    public JoinActivityService() {
        super();
    }

    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/json");

        String userID = request.getParameter("userID");
        String acIDStr = request.getParameter("acID");
        long acID = 0L;
        try {
            acID = Long.parseLong(acIDStr);
        } catch (NumberFormatException e) {
            log.error("活动id错误", e);
            acID = 0L;
        }

        String sql = "select count(userID) joinedNum from tb_activity_invitation where acID=?";
        Connection conn = DaoFactory.getDatabase().getConnection();
        List<Map<String, Object>> result = null;
        try {
            result = DBUtil.queryForMap(conn, sql, acID);
        } catch (SQLException e) {
            log.error(e);
        } finally {
            DBUtil.closeConnection(conn);
        }
        int num = 0;
        if (result != null && !result.isEmpty()) {
            num = ((Long) result.get(0).get("joinedNum")).intValue();
        }

        IActivityDao dao = DaoFactory.getActivityDao();
        Activity activity = null;
        try {
            activity = dao.getActivity(acID);
        } catch (SQLException e) {
            log.error(e);
        }
        boolean flag = false;
        if (activity != null) {
            if (num < activity.getMax()) {
                ActivityInvitation activityInvitation = new ActivityInvitation();
                activityInvitation.setAcID(acID);
                activityInvitation.setUserID(userID);
                activityInvitation.setAiState("R");
                IActivityInvitationDao aiDao = DaoFactory
                        .getIActivityInvitationDao();
                try {
                    flag = aiDao.doCreate(activityInvitation);
                } catch (SQLException e) {
                    log.error(e);
                }
            }
        }
        Gson gson = new Gson();
        String msg = gson.toJson(flag);
        PrintWriter out = response.getWriter();
        out.print(msg);
        out.flush();
        out.close();
        log.info("用户:" + userID + ", 参加活动:" + acIDStr + ", 结果:" + msg);
    }

}
